package com.qzdsoft.erpcloud.service.act.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.act.CenterCompany;
import com.qzdsoft.erpcloud.domain.basic.BasicUser;
import com.qzdsoft.erpcloud.mapper.act.CenterCompanyMapper;
import com.qzdsoft.erpcloud.mapper.basic.BasicUserMapper;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.vo.query.act.CenterCompanyInfo;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;


/**
 * Created by Yang
 */
@Service
public class CenterCompanyServiceImpl implements CenterCompanyService {

    private final static Logger log = LoggerFactory.getLogger(CenterCompanyServiceImpl.class);

    @Autowired
    private CenterCompanyMapper actCenterCompanyMapper;
    
    @Autowired
    private BasicUserMapper basicUserMapper;


    @Override
    public CenterCompany getById(Integer id) {
        return actCenterCompanyMapper.selectByPrimaryKey(id);
    }
    @Override
    public CenterCompanyInfo getByOneId(Integer id) {
        return actCenterCompanyMapper.selectByOne(id);
    }


    @Override
    public List<CenterCompanyInfo> getData(String name, String email, String contactUser, String contactPhone, Integer status, int page, int pageSize) {
        return actCenterCompanyMapper.selectData(name, email, contactUser, contactPhone, status, null, null, null, null, page, pageSize);
    }

    @Override
    public List<CenterCompany> getAllCompany() {
        return actCenterCompanyMapper.selectAll();
    }


    @Override
    public LayTableResponse<CenterCompanyInfo> selectData(String name, String email, String contactUser, String contactPhone, Integer status, Date timeFrom, Date timeTo, String recommendName, String roleName, Integer startSize, Integer pageSize) {
       
        PageHelper.startPage(startSize, pageSize);
        List<CenterCompanyInfo> centerCompanyInfo = actCenterCompanyMapper.selectData(name, email, contactUser, contactPhone, status, timeFrom, timeTo, recommendName, roleName, startSize, pageSize);
        LayTableResponse<CenterCompanyInfo> po = new LayTableResponse<CenterCompanyInfo>(centerCompanyInfo);
        return po;
    }

    @Override
    /**
     * 修改新增保存账户信息
     */
    public Response<String> edit(CenterCompanyInfo centerCompanyInfo) {
        if(null != centerCompanyInfo.getId()){        //  编辑
//            if(null != company.getEmail()){
//                SysLogin sysLogin = sysLoginService.selectForLogin(company.getEmail().toString(), null);
//                if(null == sysLogin || (sysLogin.getLoginType().equals(SysLoginTypeEnum.company.getKey()) && sysLogin.getId().equals(company.getId()))){
//                } else {
//                    return Response.getError("Email已被使用!");
//                }
//            }
//
//            if(null != company.getContact_phone()){
//                SysLogin sysLogin = sysLoginService.selectForLogin(company.getContact_phone().toString(), null);
//                if(null == sysLogin || (sysLogin.getLoginType().equals(SysLoginTypeEnum.company.getKey()) && sysLogin.getId().equals(company.getId()))){
//                } else {
//                    return Response.getError("联系人手机号已被使用!");
//                }
//            }
//              
            CenterCompany company = new CenterCompany();
            company.setId(centerCompanyInfo.getId());
            company.setName(centerCompanyInfo.getName());
            company.setEmail(centerCompanyInfo.getEmail());
            company.setAddress(centerCompanyInfo.getAddress());
            company.setRecommendUid(centerCompanyInfo.getRecommend_uid());
            company.setStatus(centerCompanyInfo.getStatus());
            company.setContactUser(centerCompanyInfo.getContact_user());
            company.setContactPhone(centerCompanyInfo.getContact_phone());
            company.setSmRate(centerCompanyInfo.getSm_rate());
            company.setVoiceRete(centerCompanyInfo.getVoice_rete());
            company.setEndTime(centerCompanyInfo.getEnd_time());
            company.setRemark(centerCompanyInfo.getRemark());
            
//            actCenterCompanyMapper.updateByPrimaryKey(company);
            actCenterCompanyMapper.updateByPrimaryKeySelective(company);
//            BasicUser basic = new BasicUser();
//            basic.setPhone(centerCompanyInfo.getContact_phone());
            
//            basic.setRoleId(centerCompanyInfo.getRole_id());
//            BasicUser basicUser =basicUserMapper.selectOne(basic);
            
////            basicUser.setStatus(centerCompanyInfo.getStatus()+"");
////            basicUser.setRoleId(centerCompanyInfo.getRole_id());
//            basicUser.setName(centerCompanyInfo.getContact_user());
//            basicUser.setEmail(centerCompanyInfo.getEmail());
//            basicUser.setPhone(centerCompanyInfo.getContact_phone());
//           
//            basicUserMapper.updateByPrimaryKeySelective(basicUser);
            
            
        } else {                            //  新增
//            SysLogin sysLogin = sysLoginService.selectForLogin(company.getEmail().toString(), null);
//            if(null != sysLogin){
//                return Response.getError("Email已被使用!");
//            }
//
//            sysLogin = sysLoginService.selectForLogin(company.getContact_phone().toString(), null);
//            if(null != sysLogin){
//                return Response.getError("联系人手机号已被使用!");
//            }
            CenterCompany company = new CenterCompany();
            company.setName(centerCompanyInfo.getName());
            company.setEmail(centerCompanyInfo.getEmail());
            company.setAddress(centerCompanyInfo.getAddress());
            company.setRecommendUid(centerCompanyInfo.getRecommend_uid());
            company.setStatus(centerCompanyInfo.getStatus());
            company.setContactUser(centerCompanyInfo.getContact_user());
            company.setContactPhone(centerCompanyInfo.getContact_phone());
            company.setSmRate(centerCompanyInfo.getSm_rate());
            company.setVoiceRete(centerCompanyInfo.getVoice_rete());
            company.setEndTime(centerCompanyInfo.getEnd_time());
            company.setRemark(centerCompanyInfo.getRemark());
            company.setCtime(new Date());
            actCenterCompanyMapper.insert(company);
            
            BasicUser baus = new BasicUser();
//            baus.setId(company.getId());
            baus.setName(centerCompanyInfo.getContact_user());
            baus.setPhone(centerCompanyInfo.getContact_phone());
            baus.setEmail(centerCompanyInfo.getEmail());
            baus.setCtime(new Date());
            baus.setRoleId(centerCompanyInfo.getRole_id());
            baus.setCompanyId(company.getId());
            baus.setStatus(centerCompanyInfo.getStatus()+"");
            
            basicUserMapper.insertSelective(baus);
            
        }
        return new Response<String>(ResultEnum.SUCCESS);
    }

    @Override
    /**
     *  根据id删除 
     */
    public Response<String> del(Integer id) {
//        CenterCompany actCenterCompany = new CenterCompany();
//        actCenterCompany.setId(id);
//        actCenterCompany.setStatus(SysUserStatusEnum.shanchu.getKey());
//        actCenterCompanyMapper.updateById(actCenterCompany);
         actCenterCompanyMapper.deleteByPrimaryKey(id);
         
//         basicUserMapper.deleteByPrimaryKey(id);
         
        return new Response<String>(ResultEnum.SUCCESS);
    }
    @Override
    public Response<String> pwdAlter(String id, String opd, String npd)
    {
        if(StringUtils.isNotBlank(opd) && StringUtils.isNotBlank(npd)) {
                BasicUser basic = new BasicUser();
                basic.setPhone(id);
                BasicUser user = basicUserMapper.selectOne(basic);
                String pwd = user.getPassword();
                if(!pwd.equals(SysUtils.string2MD5(opd))){
                    return Response.error("输入的旧密码不正确");
                }
                user.setPassword(SysUtils.string2MD5(npd));
                basicUserMapper.updateByPrimaryKey(user);
            
            
        }else{
            return Response.error("请输入密码");
        }
        return new Response<String>(ResultEnum.SUCCESS);
    }


}
